package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.oa.SynodRoom;
import com.prs.crm.service.oa.SynodRoomService;

public class SynodRoomAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private SynodRoom synodRoom;
	private SynodRoomService synodRoomService;
	private PaginationSupport pagination;
	private Integer[] ids;

	@Action("listSynodRoom")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SynodRoom.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSynodRoomService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editSynodRoom")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveSynodRoom", results = {
			@Result(name = "success", type = "redirect", location = "listSynodRoom"),
			@Result(name = "error", type = "chain", location = "editSynodRoom") })
	public String save() {
		if (synodRoom.getName() == null
				|| "".equals(synodRoom.getName().trim())) {
			addActionError("会议室不能为空");
			return ERROR;
		}
		if (synodRoom.getAddress() == null
				|| "".equals(synodRoom.getAddress().trim())) {
			addActionError("地址不能为空");
			return ERROR;
		}
		this.getSynodRoomService().save(synodRoom);
		return SUCCESS;
	}

	@Action(value = "removeSynodRoom", results = {
			@Result(name = "success", type = "redirect", location = "listSynodRoom"),
			@Result(name = "error", type = "chain", location = "listSynodRoom") })
	public String remove() {
		if (ids == null) {
			this.addActionError("请选择要删除的会议室");
			return ERROR;
		}
		for (Integer id : ids) {
			SynodRoom room = this.getSynodRoomService().get(id);
			if (room.getSynods().size() != 0) {
				this.addActionError("已有会议引用，不能删除");
				return ERROR;
			}
		}
		this.getSynodRoomService().batchRemove(ids);
		return SUCCESS;
	}

	public SynodRoom getSynodRoom() {
		return synodRoom;
	}

	public void setSynodRoom(SynodRoom synodRoom) {
		this.synodRoom = synodRoom;
	}

	public SynodRoomService getSynodRoomService() {
		return synodRoomService;
	}

	public void setSynodRoomService(SynodRoomService synodRoomService) {
		this.synodRoomService = synodRoomService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

}
